package com.jspx.txweb.dao.impl;

import com.jspx.sober.criteria.Order;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.jspx.sober.Criteria;
import com.jspx.sober.criteria.expression.Expression;
import com.jspx.sober.criteria.projection.Projections;
import com.jspx.sober.jdbc.JdbcOperations;
import com.jspx.sober.ssql.SSqlExpression;
import com.jspx.txweb.annotation.Safety;
import com.jspx.txweb.dao.UploadFileDAO;
import com.jspx.utils.ArrayUtil;
import com.jspx.utils.BeanUtil;
import com.jspx.utils.ClassUtil;
import com.jspx.utils.StringUtil;
import java.util.Date;
import java.util.List;

/**
 * Created by yuan on 2014/7/13 0013.
 * 上传DAO
 *
 */
public class UploadFileDAOImpl  extends JdbcOperations implements UploadFileDAO {
    private static final Logger log = LoggerFactory.getLogger(UploadFileDAOImpl.class);
    private Class tableClass;
    public UploadFileDAOImpl()
    {

    }

    public String getClassName()
    {
        if (tableClass==null) return StringUtil.empty;
        return tableClass.getName();
    }

    @Safety(request = false)
    public void setClassName(final String className) throws Exception {
        if (tableClass==null)
            tableClass = ClassUtil.loadClass(className);
    }

    public Class getClassType() throws Exception {
        return tableClass;
    }

    private String namespace = StringUtil.empty;
    public void setNamespace(String namespace)
    {
        this.namespace = namespace;
    }

    public String getNamespace()
    {
        return namespace;
    }

    /**
     *
     * @param id    id
     * @return 载入
     * @throws Exception
     */
    public Object load(Long id) throws Exception
    {
        return super.load(tableClass,id);
    }

    public Object get(Long id) throws Exception
    {
        return super.get(tableClass,id);
    }
    /**
     * @param ids  id
     * @return     删除
     * */
    public boolean delete(Long[] ids) throws Exception
    {
        if (ArrayUtil.isEmpty(ids)) return false;
        try
        {
            for (Long id : ids)
            {
                Object obj =  get(tableClass, id);
                if (obj != null) delete(obj);
            }
        } catch (Exception e)
        {
            log.error(e.getLocalizedMessage());
            return false;
        }
        return true;
    }

    /**
     *
     * @param hash  hash值
     * @return  返回对象
     * @throws Exception
     */
    public Object getForHash(String hash) throws Exception
    {
        return createCriteria(tableClass).add(Expression.eq("hash", hash)).objectUniqueResult(false);
    }

    public boolean haveHash(String hash) throws Exception
    {
        return createCriteria(tableClass).add(Expression.eq("hash", hash)).setProjection(Projections.rowCount()).intUniqueResult()>0;
    }


    public Object getThumbnail(long pid)
    {
        return createCriteria(tableClass).add(Expression.eq("pid", pid)).addOrder(Order.desc("sortDate")).objectUniqueResult(false);
    }

    /**
     *  得到子图列表
     * @param pid    父id
     * @return 返回子文件列表
     */
    public Object getChildFileList(long pid)
    {
        return createCriteria(tableClass).add(Expression.eq("pid", pid)).addOrder(Order.asc("sortDate")).list(false);
    }

    /**
     * @param ids id
     * @param sortType 排序
     * @return  更新排序
     */
    public boolean updateSortType(Long[] ids, int sortType) throws Exception
    {
        if (ArrayUtil.isEmpty(ids)) return false;
        try
        {
            for (Long id : ids)
            {
                Object obj =  get(tableClass, id);
                if (obj != null)
                {
                    BeanUtil.setSimpleProperty(obj, "sortType", sortType);
                    super.update(obj,new String[]{"sortType"});
                }
            }
        } catch (Exception e)
        {
            log.error("更新排序",e);
            return false;
        }
        return true;
    }

    /**
     *
     *
     * @param ids  id
     * @return 更新排序日期
     */
    public boolean updateSortDate(Long[] ids) throws Exception
    {
        if (ArrayUtil.isEmpty(ids)) return false;
        try
        {
            for (Long id : ids)
            {
                Object uploadFile = get(tableClass, id);
                if (uploadFile != null)
                {
                    BeanUtil.setSimpleProperty(uploadFile, "sortDate", new Date());
                    super.update(uploadFile,new String[]{"sortDate"});
                }
            }
        } catch (Exception e)
        {
            log.error("更新排序日期",e);
            return false;
        }
        return true;
    }


    /**
     *
     * @param field 字段
     * @param find 查询条件
     * @param term 条件
     * @param sortString 排序
     * @param uid      用户id
     * @param page     页数
     * @param count   返回数量
     * @return  返回列表
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    public List getList(String[] field,String[] find,String term,String sortString,long uid,long pid,int page, int count) throws Exception
    {
        if (StringUtil.isNULL(sortString)) sortString="createDate:D";
        Criteria criteria = createCriteria(tableClass);
        if (!ArrayUtil.isEmpty(find)&& !ArrayUtil.isEmpty(field)) {
            criteria = criteria.add(Expression.find(field, find));
        }
        if (pid>-1)
        {
            criteria = criteria.add(Expression.eq("pid", pid));
        }
        if (uid>0)
        {
            criteria = criteria.add(Expression.eq("putUid", uid));
        }
        criteria = SSqlExpression.getTermExpression(criteria, term);
        return SSqlExpression.getSortOrder(criteria, sortString).setCurrentPage(page).setTotalCount(count).list(false);
    }


    /**
     *
     * @param field 字段
     * @param find 查询条件
     * @param term 条件
     * @param uid  用户id
     * @return    得到记录条数
     * @throws Exception
     */
    public int getCount(String[] field,String[] find,String term,long uid,long pid) throws Exception
    {
        Criteria criteria = createCriteria(tableClass);
        if (!ArrayUtil.isEmpty(find)&& !ArrayUtil.isEmpty(field)) {
            criteria = criteria.add(Expression.find(field,find));
        }
        if (pid>-1)
        {
            criteria = criteria.add(Expression.eq("pid", pid));
        }
        if (uid>0)
        {
            criteria = criteria.add(Expression.eq("putUid", uid));
        }
        criteria = SSqlExpression.getTermExpression(criteria, term);
        return criteria.setProjection(Projections.rowCount()).intUniqueResult();
    }
}